package com.thinkfit.jy.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.widget.ImageView;

import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

public class ImageLoadUtils {

    private static ImageLoadUtils imageLoadUtils;

    boolean isFist = true;

    private static Context mContext;

    public static ImageLoadUtils getInstance(Context context){
        mContext = context;
        if(imageLoadUtils == null){
            imageLoadUtils = new ImageLoadUtils();
        }
        return imageLoadUtils;
    }

    public void setImageRadius(ImageView iv,int imageId,int radius){
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), imageId);
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
        roundedBitmapDrawable.setCornerRadius(radius);
        roundedBitmapDrawable.setAntiAlias(true);
        iv.setImageDrawable(roundedBitmapDrawable);
    }

    public void setImageRadius(ImageView iv, Drawable drawable, int radius){
//        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
//        Bitmap bitmap = bitmapDrawable.getBitmap();
//        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
//        roundedBitmapDrawable.setCornerRadius(radius);
//        roundedBitmapDrawable.setAntiAlias(true);
//        iv.setImageDrawable(roundedBitmapDrawable);
        RequestOptions options = new RequestOptions()
                .bitmapTransform(new RoundedCorners(dip2px(mContext, radius)));
        Glide.with(mContext)
                .load(drawable)
                .apply(options)
                .into(iv);
    }

    /**
     * 加载网络图（圆角）
     * @param iv
     * @param url
     * @param imageId
     * @param radius
     */
    public void setImageUtlRadius(ImageView iv,String url,int imageId,int radius){
        RequestOptions options = new RequestOptions()
                .bitmapTransform(new RoundedCorners(dip2px(mContext, radius)));
        Glide.with(mContext)
                .load(url)
                .apply(options)
                .into(iv);
    }

    /**
     * 高斯模糊
     * @param source
     * @param radius
     * @return
     */
    public static Bitmap applyGaussianBlur(Bitmap source, float radius){
        Bitmap blurredBitmap = Bitmap.createBitmap(source.getWidth(),source.getHeight(),Bitmap.Config.ARGB_8888);
        RenderScript renderScript = RenderScript.create(mContext);
        Allocation input = Allocation.createFromBitmap(renderScript, source);
        Allocation output = Allocation.createFromBitmap(renderScript, blurredBitmap);
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        blurScript.setRadius(radius);
        blurScript.setInput(input);
        blurScript.setInput(output);
        output.copyTo(blurredBitmap);
        renderScript.destroy();

        return blurredBitmap;
    }


    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
